﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace Hisun.View.DataExchange
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        ICSharpCode.SharpZipLib.WebReference.SynBaseItf cx = new ICSharpCode.SharpZipLib.WebReference.SynBaseItf();
        string strCon1 = System.Configuration.ConfigurationManager.ConnectionStrings["hisunConnectionString"].ConnectionString;
        //string strCon1 = "Data Source=ABC-RB\\SQLEXPRESS;Initial Catalog=hisuntest;Integrated Security=True";
     
        protected void Page_Load(object sender, EventArgs e)
        {
            //StreamReader sr = new StreamReader(@"C:\text.txt");
            //string[] st = new string[2];
            ////string strLine = sr.ReadLine();

            //while ((st[0] = sr.ReadLine()) != null)
            //{
            //    st[1] = sr.ReadLine();
            //    string[] str1 = st[0].Split(' ');

            //};
            if (!IsPostBack)
            {
                if (Session["ip"] != null && Session["cid"] != null && Session["aid"] != null)
                {
                    TextBox2.Text = Session["ip"].ToString();
                    TextBox9.Text = Session["aid"].ToString();
                    TextBox10.Text = Session["cid"].ToString();

                    string s = "http://" + Session["ip"] + "/uapws/service/nc.itf.hz.syn.SynBaseItf";
                    cx.Url = s;
                }
                else
                {
                    //TextBox2.Text = "172.16.1.41";
                    TextBox2.Text = "172.16.1.41:8041";
                    TextBox9.Text = "001";
                    TextBox10.Text = "101";
                    string s = "http://" + TextBox2.Text + "/uapws/service/nc.itf.hz.syn.SynBaseItf";
                    cx.Url = s;
                }
            }
            else
            {
                    string s = "http://" + TextBox2.Text + "/uapws/service/nc.itf.hz.syn.SynBaseItf";
                    cx.Url = s;
            }
           
        }

        //获取NC成本账户
        protected void Button_costdata_Click(object sender, EventArgs e)       
        {
            //dataclear();
            //string slj = cx.Url;
            String s = cx.getNC_costData(TextBox9.Text,TextBox10.Text);      //获取NC成本账户
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            DataTable dt = new DataTable();
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("成本账户编码");//成本账户编码
            dt.Columns.Add("成本账户名称");//成本账户名称
            dt.Columns.Add("是否封存");//是否封存
            dt.Columns.Add("pk_cost");
            //DataSet ds = new DataSet();
            //DataTable dt1 = new DataTable();
            //ds.Tables.Add(dt1);
            //dt1.Columns.Add("pk_NCdata");
            //dt1.Columns.Add("同步成功");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            switch (el.Name)
                            {
                                case "pk_corp":
                                    dr["公司号"] = el.InnerText;
                                    break;
                                case "ncaccount":
                                    dr["帐套号"] = el.InnerText;
                                    break;
                                case "costcode":
                                    dr["成本账户编码"] = el.InnerText;
                                    break;
                                case "costname":
                                    dr["成本账户名称"] = el.InnerText;
                                    break;
                                case "isarchive":
                                    dr["是否封存"] = el.InnerText;
                                    break;
                                case "pk_cost":
                                    dr["pk_cost"] = el.InnerText;
                                    break;

                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_CostInv";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();

                foreach (DataRow dr in dt.Rows)
                {
                    //DataRow dr1=dt1.NewRow();
                    string sql = @"if exists (select * from TABC_NC_CostInv where 成本账户编码=@costcode ) 
                                update TABC_NC_CostInv  set 公司号=@pk_corp,帐套号=@ncaccount,成本账户名称=@costname,是否封存=@isarchive where 成本账户编码=@costcode 
                                else insert into TABC_NC_CostInv  (公司号,帐套号,成本账户编码,成本账户名称,是否封存) values(@pk_corp,@ncaccount,@costcode,@costname,@isarchive)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@costcode", dr["成本账户编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@costname", dr["成本账户名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter parq = new SqlParameter("@isarchive", dr["是否封存"]);
                    cmd.Parameters.Add(parq);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    //dr1["pk_NCdata"] = dr["pk_cost"];
                    //dr1["同步成功"] = "1";
                    //dt1.Rows.Add(dr1);
                };
                //System.Xml.XmlDataDocument _XmlDataDocument = new System.Xml.XmlDataDocument(ds);
                //string re = _XmlDataDocument.OuterXml;
                //cx.setNC_Data("0", re);
                Response.Write("<script>alert('同步成功,获取" + n + "条数据,同步" + i + "条');</script>");
            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }
        }

        //获取NC成本账户与存货编码对照表
        protected void Button_costinv_Click(object sender, EventArgs e)
        {
            //dataclear();
            // WebReference.SynBaseItf cx = new WebReference.SynBaseItf();
            String s = cx.getNC_costinv(TextBox9.Text, TextBox10.Text);//获取NC成本账户与存货编码对照表
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            //DataSet ds=new DataSet();
            DataTable dt = new DataTable();
            //DataTable dt1 = new DataTable();
            //ds.Tables.Add(dt1);
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("成本账户编码");
            dt.Columns.Add("成本账户名称");
            dt.Columns.Add("存货编码"); //存货编码
            dt.Columns.Add("存货名称"); //存货名称
            dt.Columns.Add("型号"); 
            dt.Columns.Add("pk_costinv");
            //dt1.Columns.Add("pk_costinv");
            //dt1.Columns.Add("同步成功");
            
            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                 if (element.Name.ToLower() == "bill")
                {
                XmlNodeList nodelist = element.ChildNodes;
                if (nodelist.Count > 0)
                {
                    DataRow dr = dt.NewRow();
                    foreach (XmlElement el in nodelist)
                    {
                        switch (el.Name)
                        {
                            case "ncaccount":
                                dr["帐套号"] = el.InnerText;
                                break;
                            case "costcode":
                                dr["成本账户编码"] = el.InnerText;
                                break;
                            case "costname":
                                dr["成本账户名称"] = el.InnerText;
                                break;
                            case "invcode":
                                dr["存货编码"] = el.InnerText;
                                break;
                            case "invname":
                                dr["存货名称"] = el.InnerText;
                                break;
                            case "invspec":
                                dr["型号"] = el.InnerText;
                                break;
                            case "pk_costinv":
                                dr["pk_costinv"] = el.InnerText;
                                break;
                            case "pk_corp":
                                dr["公司号"] = el.InnerText;
                                break;
                        }
                    }
                    dt.Rows.Add(dr);
                }
             }


            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
               // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                foreach (DataRow dr in dt.Rows)
                {
                   
                    string sql = @"if exists ( select * from TABC_NC_CostInv where 成本账户编码=@costcode )
                            update TABC_NC_CostInv set  帐套号=@ncaccount,成本账户名称=@costname,存货编码=@invcode,存货名称=@invname,型号=@invspec,公司号=@pk_corp where 成本账户编码=@costcode                      
                            else insert into TABC_NC_CostInv (公司号,帐套号,成本账户编码,成本账户名称,存货编码,存货名称,型号) 
                                        values(@pk_corp,@ncaccount,@costcode,@costname,@invcode,@invname,@invspec)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@costcode", dr["成本账户编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@costname", dr["成本账户名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter parq = new SqlParameter("@invcode", dr["存货编码"]);
                    cmd.Parameters.Add(parq);
                    SqlParameter paro = new SqlParameter("@invname", dr["存货名称"]);
                    cmd.Parameters.Add(paro);
                    SqlParameter parr = new SqlParameter("@invspec", dr["型号"]);
                    cmd.Parameters.Add(parr);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    //dr1["pk_costinv"] = dr["pk_costinv"];
                    //dr1["同步成功"] = "1";
                    //dt1.Rows.Add(dr1);                                      
                };
                 
                //System.Xml.XmlDataDocument _XmlDataDocument = new System.Xml.XmlDataDocument(ds);
                //string re = _XmlDataDocument.OuterXml;
                //cx.setNC_Data("1",re);
                Response.Write("<script>alert('同步成功,获取" + n + "条数据,同步" + i + "条');</script>");


            }
            catch { Response.Write("同步失败"); };
        }
        
        //获取NC部门档案，新建表TABC_NC_Dept
        protected void Button_dept_Click(object sender, EventArgs e)
        {
            //dataclear();
            //WebReference.SynBaseItf cx = new WebReference.SynBaseItf();
            String s = cx.getNC_deptdoc(TextBox9.Text,TextBox10.Text);  //获取NC部门档案
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            DataTable dt = new DataTable();
            //dt.Columns.Add("pk_deptdoc");
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("部门编码"); //部门编号
            dt.Columns.Add("部门名称"); //部门名称
            //dt.Columns.Add("pk_fathedept");  //上级部门

            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                 if (element.Name.ToLower() == "bill")
                {
                XmlNodeList nodelist = element.ChildNodes;
                if (nodelist.Count > 0)
                {
                    DataRow dr = dt.NewRow();
                    foreach (XmlElement el in nodelist)
                    {
                        switch (el.Name)
                        {
                            //case "pk_deptdoc":
                            //    dr["pk_deptdoc"] = el.InnerText;
                            //    break;
                            case "pk_corp":
                                dr["公司号"] = el.InnerText;
                                break;
                            case "ncaccount":
                                dr["帐套号"] = el.InnerText;
                                break;
                            case "deptcode":
                                dr["部门编码"] = el.InnerText;
                                break;
                            case "deptname":
                                dr["部门名称"] = el.InnerText;
                                break;
                            //case "pk_fathedept":
                            //    dr["pk_fathedept"] = el.InnerText;
                            //    break;


                        }
                    }
                    dt.Rows.Add(dr);
                }

                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {

                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_Dept";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();

                foreach (DataRow dr in dt.Rows)
                {
                    string sql = @"if exists (select * from TABC_NC_Dept where 部门编码=@deptcode) 
                                update TABC_NC_Dept set 帐套号=@ncaccount,部门名称=@deptname,公司号=@pk_corp where 部门编码=@deptcode
                                 else insert into TABC_NC_Dept values(@pk_corp,@ncaccount,@deptcode,@deptname)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb= new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@deptcode", dr["部门编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@deptname", dr["部门名称"]);
                    cmd.Parameters.Add(parp);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();


                };
                Response.Write("<script>alert('同步成功,获取" + n + "条数据,同步" + i + "条');</script>");

            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }
        }

        //获取NC存货档案
        protected void Button_invbas_Click(object sender, EventArgs e)
        {
            
            //dataclear();
            //WebReference.SynBaseItf cx = new WebReference.SynBaseItf();
            String s = cx.getNC_invbasdoc(TextBox9.Text, TextBox10.Text);   //获取NC存货档案
            s = s.Replace("&", "&amp;");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            //doc.Save("C:/Users/asus/Desktop/inv.xml");
            DataSet ds=new DataSet();
            DataTable dt1 = new DataTable();
            ds.Tables.Add(dt1);
            // ds.ReadXml(reader);
            DataTable dt = new DataTable();
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("存货编码"); //存货编码
            dt.Columns.Add("存货名称"); //存货名称
            dt.Columns.Add("计划价");
            dt.Columns.Add("pk_invbasdoc");
            dt1.Columns.Add("pk_NCdata");
            dt1.Columns.Add("同步成功");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {

                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            switch (el.Name)
                            {
                                case "pk_corp":
                                    dr["公司号"] = el.InnerText;
                                    break;
                                case "ncaccount":
                                    dr["帐套号"] = el.InnerText;
                                    break;
                                case "invcode":
                                    dr["存货编码"] = el.InnerText;
                                    break;
                                case "invname":
                                    dr["存货名称"] = el.InnerText;
                                    break;
                                case "jhj":
                                    dr["计划价"] = el.InnerText;
                                    break;
                                case "pk_invbasdoc":
                                    dr["pk_invbasdoc"] = el.InnerText;
                                    break;
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            };
            GridView1.DataSource = null;
            GridView1.DataBind();
            //GridView1.DataSource = dt;
           // GridView1.DataBind();
            int n = dt.Rows.Count;
            int x = 0;
            try
            {

                SqlConnection sqlCon1;
               // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();
                //string sql1 = @"delete from TABC_NC_Inv";
                //SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                //int i = cmd1.ExecuteNonQuery();
                //cmd1.Dispose();
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow dr1 = dt1.NewRow();
                    //string sql = @"if exists (select * from TABC_NC_Inv where 存货编码=@invcode) 
                    //                update TABC_NC_Inv set 公司号=@pk_corp,存货名称=@invname,计划价=@jhj where 存货编码=@invcode 
                    //                else insert into TABC_NC_Inv  values(@pk_corp,@invcode,@invname,@jhj)";
                    string sql = @"insert into TABC_NC_Inv values(@pk_corp,@ncaccount,@invcode,@invname,@jhj)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@invcode", dr["存货编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@invname", dr["存货名称"]);
                    cmd.Parameters.Add(parp);
                    dr["计划价"] = nullto0(Convert.ToString(dr["计划价"]));
                    SqlParameter parm = new SqlParameter("@jhj", DbType.Decimal);
                    parm.Precision = 18;
                    parm.Scale = 4;
                    parm.Value = Decimal.Parse(dr["计划价"].ToString()).ToString();
                    //SqlParameter paro = new SqlParameter("@jhj", dr["jhj"]);
                    cmd.Parameters.Add(parm);
                    x += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    dr1["pk_NCdata"] = dr["pk_invbasdoc"];
                    dr1["同步成功"] = "0";
                    dt1.Rows.Add(dr1); 
                };
            System.Xml.XmlDataDocument _XmlDataDocument = new System.Xml.XmlDataDocument(ds);
            string re = _XmlDataDocument.OuterXml;
            //re = re.Replace("table1","table0");
            cx.setNC_Data(TextBox9.Text,TextBox10.Text,"2",re);
            Response.Write("<script>alert('同步成功,获取" + n + "条,同步" + x + "条');</script>");

            }
            catch { Response.Write("<script>alert('同步成功,获取" + n + "条,同步" + x + "条');</script>"); }
        }
        protected string nullto0(string s)
        {
            if (s == "null")
            {
                s = "0";
            }
            return s;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string s = TextBox1.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_Inv where 存货名称 like '%"+s+"%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button_account_Click(object sender, EventArgs e)
        {
            String s = cx.getNC_accsubj(TextBox9.Text, TextBox10.Text);      //获取NC会计科目
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            DataTable dt = new DataTable();
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("科目编码");
            dt.Columns.Add("科目名称");
            dt.Columns.Add("科目级次");
            dt.Columns.Add("是否末级");
            dt.Columns.Add("科目方向");
            dt.Columns.Add("科目类型");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            switch (el.Name)
                            {
                                case "pk_corp":
                                    dr["公司号"] = el.InnerText;
                                    break;
                                case "ncaccount":
                                    dr["帐套号"] = el.InnerText;
                                    break;
                                case "subjcode":
                                    dr["科目编码"] = el.InnerText;
                                    break;
                                case "subjname":
                                    dr["科目名称"] = el.InnerText;
                                    break;
                                case "endflag":
                                    dr["是否末级"] = el.InnerText;
                                    break;
                                case "subjlev":
                                    dr["科目级次"] = el.InnerText;
                                    break;
                                case "balanorient":
                                    dr["科目方向"] = el.InnerText;
                                    break;
                                case "pk_subjtype":
                                    dr["科目类型"] = el.InnerText;
                                    break;
                                    
                                    

                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            };
            DataRow[] drArr = dt.Select("科目编码 LIKE '51%'");
            DataTable dtNew = dt.Clone();
            for (int x = 0; x < drArr.Length; x++)
            {
                dtNew.ImportRow(drArr[x]);//ImportRow 是复制
            }


            GridView1.DataSource = dtNew;
            GridView1.DataBind();
            int n = dtNew.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_accsubj";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();

                foreach (DataRow dr in dtNew.Rows)
                {
                    //DataRow dr1=dt1.NewRow();
                    string sql = @"if exists (select * from TABC_NC_accsubj where 科目编码=@subjcode ) 
                                update TABC_NC_accsubj  set 公司号=@pk_corp,帐套号=@ncaccount,科目名称=@subjname,是否末级=@endflag,科目级次=@subjlev,科目类型=@pk_subjtype,科目方向=@balanorient where 科目编码=@subjcode 
                                else insert into TABC_NC_accsubj values(@pk_corp,@ncaccount,@subjcode,@subjname,@subjlev,@endflag,@balanorient,@pk_subjtype)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@subjcode", dr["科目编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@subjname", dr["科目名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter parq = new SqlParameter("@subjlev", dr["科目级次"]);
                    cmd.Parameters.Add(parq);
                    SqlParameter par1 = new SqlParameter("@endflag", dr["是否末级"]);
                    cmd.Parameters.Add(par1);
                    SqlParameter par2 = new SqlParameter("@balanorient", dr["科目方向"]);
                    cmd.Parameters.Add(par2);
                    SqlParameter par3 = new SqlParameter("@pk_subjtype", dr["科目类型"]);
                    cmd.Parameters.Add(par3);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    //dr1["pk_NCdata"] = dr["pk_cost"];
                    //dr1["同步成功"] = "1";
                    //dt1.Rows.Add(dr1);
                };
                //System.Xml.XmlDataDocument _XmlDataDocument = new System.Xml.XmlDataDocument(ds);
                //string re = _XmlDataDocument.OuterXml;
                //cx.setNC_Data("0", re);
                Response.Write("<script>alert('同步成功,获取" + n + "条数据,同步" + i + "条');</script>");
            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }

        }

        protected void Button_expense_Click(object sender, EventArgs e)
        {
            String s = cx.getNC_costsubj(TextBox9.Text, TextBox10.Text);    
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            DataTable dt = new DataTable();
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("项目编码");
            dt.Columns.Add("项目名称");
            dt.Columns.Add("备注");
            dt.Columns.Add("助记码");
            dt.Columns.Add("是否费用项目");
            dt.Columns.Add("上级收支项目");
            dt.Columns.Add("对应现金流量项目");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            switch (el.Name)
                            {
                                case "pk_corp":
                                    dr["公司号"] = el.InnerText;
                                    break;
                                case "ncaccount":
                                    dr["帐套号"] = el.InnerText;
                                    break;
                                case "costcode":
                                    dr["项目编码"] = el.InnerText;
                                    break;
                                case "costname":
                                    dr["项目名称"] = el.InnerText;
                                    break;
                                case "memo":
                                    dr["备注"] = el.InnerText;
                                    break;
                                case "mnecode":
                                    dr["助记码"] = el.InnerText;
                                    break;
                                case "isexpensepro":
                                    dr["是否费用项目"] = el.InnerText;
                                    break;
                                case "pk_parent":
                                    dr["上级收支项目"] = el.InnerText;
                                    break;
                                case "pk_cashflow":
                                    dr["对应现金流量项目"] = el.InnerText;
                                    break;
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_Costsubj";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();

                foreach (DataRow dr in dt.Rows)
                {
                    //DataRow dr1=dt1.NewRow();
                    string sql = @"if exists (select * from TABC_NC_Costsubj where 项目编码=@costcode and 公司号=@pk_corp ) 
                                update TABC_NC_Costsubj  set 帐套号=@ncaccount,项目名称=@costname,备注=@memo,助记码=@mnecode,是否费用项目=@isexpensepro,上级收支项目=@pk_parent,
                                对应现金流量项目=@pk_cashflow where 项目编码=@costcode and 公司号=@pk_corp
                                else insert into TABC_NC_Costsubj values(@pk_corp,@ncaccount,@costcode,@costname,@memo,@mnecode,@isexpensepro,@pk_parent,@pk_cashflow)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter para = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parn = new SqlParameter("@costcode", dr["项目编码"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parp = new SqlParameter("@costname", dr["项目名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter parq = new SqlParameter("@memo", dr["备注"]);
                    cmd.Parameters.Add(parq);
                    SqlParameter par1 = new SqlParameter("@mnecode", dr["助记码"]);
                    cmd.Parameters.Add(par1);
                    SqlParameter par2 = new SqlParameter("@isexpensepro", dr["是否费用项目"]);
                    cmd.Parameters.Add(par2);
                    SqlParameter par3 = new SqlParameter("@pk_parent", dr["上级收支项目"]);
                    cmd.Parameters.Add(par3);
                    SqlParameter par4 = new SqlParameter("@pk_cashflow", dr["对应现金流量项目"]);
                    cmd.Parameters.Add(par4);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    //dr1["pk_NCdata"] = dr["pk_cost"];
                    //dr1["同步成功"] = "1";
                    //dt1.Rows.Add(dr1);
                };
                //System.Xml.XmlDataDocument _XmlDataDocument = new System.Xml.XmlDataDocument(ds);
                //string re = _XmlDataDocument.OuterXml;
                //cx.setNC_Data("0", re);
                Response.Write("<script>alert('同步成功,获取" + n + "条数据,同步" + i + "条');</script>");
            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string s = TextBox6.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_accsubj where 科目名称 like '%" + s + "%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string s = TextBox4.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_Costsubj where 项目名称 like '%" + s + "%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string s = TextBox5.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_CostInv where 成本账户编码 like '%" + s + "%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            string s = TextBox7.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_CostInv where 成本账户名称 like '%" + s + "%' or 存货名称 like '%" + s + "%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            string s = TextBox8.Text;
            SqlConnection sqlCon1;
            // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_Dept where 部门名称 like '%" + s + "%' ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {

            if (TextBox9.Text == "" || TextBox10.Text == ""|| TextBox2.Text=="")
            { Response.Write("<script>alert('输入有误,请重新输入');</script>"); }
            else
            {
                Session["aid"] = TextBox9.Text;
                Session["cid"] = TextBox10.Text;
                Session["ip"] = TextBox2.Text;
                Response.Write("<script>alert('修改成功');</script>");
                TextBox10.ReadOnly = true;
                TextBox9.ReadOnly = true;
                TextBox2.ReadOnly = true;
            }
           
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            TextBox10.ReadOnly = false;
            TextBox9.ReadOnly = false;
            TextBox2.ReadOnly = false;

        }
    }
}
